<%@ page import="com.one.pojo.Books" %>
<%@ page import="java.util.List" %><%--
  Created by IntelliJ IDEA.
  User: LJ
  Date: 2022/4/25
  Time: 14:00
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script type="text/javascript"></script>
    <script>
        let b=0;
        window.onload=function (){

            <%
              if (request.getAttribute("goLogin")!=null){
              %>
                alert("账户或密码错误");
            <%}%>
            <%
            HttpSession session1 = request.getSession();
            Object attribute = session1.getAttribute("name");
            Object id = session1.getAttribute("id");
            if (attribute!=null){
            %>
            b=1;
            console.log(document.getElementById("login").valueOf());
            document.getElementById("login").innerText="当前用户:<%=attribute%>";
            document.getElementById("register").innerText="注销";
            <% } %>
        }
        function login() {
            <%
            if (attribute==null){
            %>
            window.location.href = "login.html";
            <% } %>
        }

        function cart(bookName,Price){
            <%
           if (attribute==null){
           %>
          alert("请先登录");
            <% }else { %>
            var s = prompt("输入你要购买的数量");
            if (s>0){
                alert("已添加到购物车");
                var number = Date.now();
                window.location.href="saveCart?number="+s+
                    "&&datetime="+number+
                    "&&bookName="+bookName+
                    "&&price="+Price+
                    "&&start="+1+
                    "&&userid="+<%=id%>;
            }
            <%}%>
        }
       function goCart(){
           <%
        if (attribute==null){
        %>
           alert("请先登录");
           <% }else { %>
           window.location.href="goCare";
           <%
        }%>

        }
        function register(go){
            console.log(go);
            console.log(b);
            <%
           if (attribute==null){
           %>
            window.location.href="register.jsp";
            <% }  %>
            if (b==1 && go==5){
                window.location.href="index?haha=1";
            }
        }
        function goBuy(){
            <%
             if (attribute==null){
             %>
            alert("请先登录");
            <% }else { %>
            location.href="buy.html";
            <%}%>

         }
     </script>
 </head>
 <body>
 <table>
     <tr>
         <td colspan="1" style="color: red" onclick="login()" id="login">登录</td>
         <td colspan="2" style="color: red" onclick="register(5)" id="register" >注册</td>
         <td style="color: red" onclick="goCart()">购物车</td>
     </tr>
     <tr>
         <td>编号</td>
         <td>书名</td>
         <td>价格</td>
         <td>操作</td>
     </tr>
     <%
         List<Books> list=(List<Books>)(request.getAttribute("list"));
         for (int i = 0; i < list.size(); i++) {

     %>
        <tr>
        <td><%=list.get(i).getId()  %></td>
        <td><%=list.get(i).getBookName()  %></td>
        <td><%=list.get(i).getPrice()  %></td>
        <td><button onclick="goBuy()">购买</button><button onclick="cart('<%=list.get(i).getBookName()%>','<%=list.get(i).getPrice()%>')">加入购物车</button></td>
        </tr>
    <% } %>
</table>
</body>
</html>
